<?php
/**
 * @package YARBB
 * @copyright Авторские права (C) 2009 www.yar-it.com. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * YARBB! - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * Для получения информации о используемых расширениях и замечаний об авторском праве, смотрите файл copyright.php.
 */
// запрет прямого доступа
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );



// Let's test if it is update/creation of annonce

if (isset($_POST['annonce_id'])) {

	if (isset($_POST['annonce_exists'])) {

		if ($_POST['new_annonce_text']=='') {
//если пусто, то удаляем анонс
			$sql = ('DELETE FROM #__forum_annonces WHERE forum_id=\''.$database->getEscaped($_POST['annonce_id']).'\'');
			$database->setQuery($sql);
			$database->Query($sql);

		} else {

			$isVisible = ($_POST['isVisible'] == 'True') ? "1":"0";
//обновляем анонс
			$sql = 'UPDATE #__forum_annonces SET forum_annonce=\''.$database->getEscaped($_POST['new_annonce_text']).'\', forum_annonce_visible=\''.$isVisible.'\' WHERE forum_id=\''.$database->getEscaped($_POST['annonce_id']).'\'';
			$database->setQuery($sql);
			$database->Query($sql);
		}

	}
	else {
		if ($_POST['new_annonce_text']!='') //новый анонс

			$sql = 'INSERT INTO #__forum_annonces  (forum_id, forum_annonce, forum_annonce_visible) VALUES (\''.$database->getEscaped($_POST['annonce_id']).'\',\''.$database->getEscaped($_POST['new_annonce_text']).'\',\''.$database->getEscaped($_POST['annonce_visible']).'\')';
		$database->setQuery($sql);
		$database->Query($sql);
	}
}
?>
<table class="adminheading">
	<tr>
		<th class="menus"> <span><?php echo _Y_FOR_ANONC; ?></span> </th>
	</tr>
</table>
<span class="message"> <?php echo _Y_ANFORDESC;?></span>
<div id="forumannonces" class="blockform">
	<?php

// Выводим категории и форумы

	$sql = 'SELECT c.id AS cid, c.cat_name, c.disp_position, f.id
				AS fid, f.forum_name, f.forum_desc, f.disp_position, fa.forum_annonce, fa.forum_annonce_visible 
				FROM #__forum_categories AS c 
				INNER JOIN #__forum_forums AS f ON c.id=f.cat_id 
				LEFT JOIN #__forum_annonces as fa ON (fa.forum_id=f.id) 
				ORDER BY c.disp_position, c.id, f.disp_position';
	$database->setQuery($sql);
	$cur_forum_result = $database->loadAssocList();

	$cur_category = 0;

	$cat_count = 0;

	foreach ($cur_forum_result as $cur_forum) {

		if ($cur_forum['cid'] != $cur_category)	// A new category since last iteration?
		{

			if ($cur_category != 0)

				echo "\t\t\t".'</tbody>'."\n\t\t\t".'</table>'."\n\t\t".''."\n\t".''."\n".''."\n\n";

			++$cat_count;

			?>

	<script type="text/javascript">
		<!--//<![CDATA[
		var old_annonce_text="";

		function editAnnonce(fid, checked) {

			if (document.getElementById) {

				// Already editing that announce, so nothing to do

				if (document.getElementById('new_forum_'+fid)) {

					return (1);

				}

				// Just one edition at a time

				if (document.getElementById('new_annonce_text')) {

					alert("<?php echo _Y_ANWARN; ?>");

					return (2);

				}

				if (! document.getElementById('new_forum_'+fid)) {

					var annonceText = document.getElementById('forum_'+fid).innerHTML;

					var newAnnonceText;

					old_annonce_text=annonceText;

					newAnnonceText = "<div id='new_forum_"+fid+"'><form id=\"form\" action=\"<?php echo $_SERVER['REQUEST_URI'] ?>\" method=\"post\"><input type=\"hidden\" name=\"annonce_id\" value=\""+fid+"\"><textarea cols='80' name=\"new_annonce_text\" id=\"new_annonce_text\">"+annonceText+"</textarea><div><?php echo _CMN_PUBLISHED; ?>?<input type=\"checkbox\" name=\"isVisible\" id=\"isVisible\" value=\"True\" "+checked+"></div>";

					if (annonceText!='') {

						newAnnonceText += "<input type=\"hidden\" name=\"annonce_exists\" value=\"1\">";

					}

					newAnnonceText += "</div><div><input type='button' value='<?php echo _CMN_SAVE; ?>' onclick='saveAnnonceModification("+fid+")'><input type='button' value='<?php echo _CMN_CANCEL; ?>' onclick='cancelAnnonceModification("+fid+")'></div></form></div>";

					document.getElementById('forum_'+fid).innerHTML = newAnnonceText;

				}

			}

		}

		function saveAnnonceModification(fid) {

			old_annonce_text="";

			document.getElementById('form').submit();

		}

		function cancelAnnonceModification(fid) {

			if (document.getElementById) {

				var currentAnnonce=document.getElementById('forum_'+fid);

				currentAnnonce.innerHTML = old_annonce_text;

				old_annonce_text="";

			}

		}
		//]]>-->
	</script>
</div>
<div id="idx<?php echo $cat_count ?>" class="blocktable">

	<h2><span><?php echo yarbb_htmlspecialchars($cur_forum['cat_name']) ?></span></h2>

	<table cellspacing="0" class="adminform">

		<thead>

			<tr>

				<th  scope="col"><?php echo _Y_FORUMS; ?></th>

			</tr>

		</thead>

		<tbody>

					<?php

					$cur_category = $cur_forum['cid'];

				}

				$checked = ($cur_forum['forum_annonce_visible'] == 1) ? "checked": "";

				$forum_field = '<div style="cursor:pointer"><h3><a onclick="editAnnonce(\''.$cur_forum['fid'].'\',\''.$checked.'\')">'.yarbb_htmlspecialchars($cur_forum['forum_name']).'</a></h3></div>';

//		$forum_field .= "\n\t\t\t\t\t\t\t\t<div id='forum_".$cur_forum['fid']."'>Ceci est un test</div>";

				$forum_field .= "\n\t\t\t\t\t\t\t\t<div id='forum_".$cur_forum['fid']."'>";

				if ($cur_forum['forum_annonce'] != '') {

					$forum_field .=$cur_forum['forum_annonce'];

				}

				$forum_field .="</div>";

				?>

			<tr<?php if ($item_status != '') echo ' class="'.$item_status.'"'; ?>>
				<td class="tcl">
					<div class="intd">
						<div class="tclcon">
								<?php echo $forum_field."\n" ?>
						</div>
					</div>
				</td>
			</tr>
				<?php
			}

// Did we output any categories and forums?
	
			if ($cur_category > 0)

				echo "\t\t\t".'</tbody>'."\n\t\t\t".'</table>'."\n\t\t".'</div>'."\n\t".'</div>'."\n".'</div>'."\n\n";

			else

				echo '<p>'._Y_ANFORMEMPT.'</p>';

			?>

			</div>